<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ib_insync.ticker &mdash; ib_insync 0.9.82 documentation</title>
      <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
    <link rel="canonical" href="https://ib_insync.readthedocs.io_modules/ib_insync/ticker.html"/>
  
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/sphinx_highlight.js"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> ib_insync
          </a>
              <div class="version">
                0.9
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../readme.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../api.html">API docs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notebooks.html">Notebooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../recipes.html">Code recipes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../code.html">Source code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../links.html">Links</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">ib_insync</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../index.html">Module code</a> &raquo;</li>
      <li>ib_insync.ticker</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for ib_insync.ticker</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Access to realtime market information.&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">dataclasses</span> <span class="kn">import</span> <span class="n">dataclass</span><span class="p">,</span> <span class="n">field</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">ClassVar</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">Union</span>

<span class="kn">from</span> <span class="nn">eventkit</span> <span class="kn">import</span> <span class="n">Event</span><span class="p">,</span> <span class="n">Op</span>

<span class="kn">from</span> <span class="nn">ib_insync.contract</span> <span class="kn">import</span> <span class="n">Contract</span>
<span class="kn">from</span> <span class="nn">ib_insync.objects</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">DOMLevel</span><span class="p">,</span> <span class="n">Dividends</span><span class="p">,</span> <span class="n">FundamentalRatios</span><span class="p">,</span> <span class="n">MktDepthData</span><span class="p">,</span>
    <span class="n">OptionComputation</span><span class="p">,</span> <span class="n">TickByTickAllLast</span><span class="p">,</span> <span class="n">TickByTickBidAsk</span><span class="p">,</span> <span class="n">TickByTickMidPoint</span><span class="p">,</span>
    <span class="n">TickData</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">ib_insync.util</span> <span class="kn">import</span> <span class="n">dataclassRepr</span><span class="p">,</span> <span class="n">isNan</span>

<span class="n">nan</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;nan&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="Ticker"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Ticker">[docs]</a><span class="nd">@dataclass</span>
<span class="k">class</span> <span class="nc">Ticker</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Current market data such as bid, ask, last price, etc. for a contract.</span>

<span class="sd">    Streaming level-1 ticks of type :class:`.TickData` are stored in</span>
<span class="sd">    the ``ticks`` list.</span>

<span class="sd">    Streaming level-2 ticks of type :class:`.MktDepthData` are stored in the</span>
<span class="sd">    ``domTicks`` list. The order book (DOM) is available as lists of</span>
<span class="sd">    :class:`.DOMLevel` in ``domBids`` and ``domAsks``.</span>

<span class="sd">    Streaming tick-by-tick ticks are stored in ``tickByTicks``.</span>

<span class="sd">    For options the :class:`.OptionComputation` values for the bid, ask, resp.</span>
<span class="sd">    last price are stored in the ``bidGreeks``, ``askGreeks`` resp.</span>
<span class="sd">    ``lastGreeks`` attributes. There is also ``modelGreeks`` that conveys</span>
<span class="sd">    the greeks as calculated by Interactive Brokers&#39; option model.</span>

<span class="sd">    Events:</span>
<span class="sd">        * ``updateEvent`` (ticker: :class:`.Ticker`)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">events</span><span class="p">:</span> <span class="n">ClassVar</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;updateEvent&#39;</span><span class="p">,)</span>

    <span class="n">contract</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Contract</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">time</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">marketDataType</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">minTick</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">bid</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">bidSize</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">bidExchange</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">ask</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">askSize</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">askExchange</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">last</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">lastSize</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">lastExchange</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">prevBid</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">prevBidSize</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">prevAsk</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">prevAskSize</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">prevLast</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">prevLastSize</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">volume</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="nb">open</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">high</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">low</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">close</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">vwap</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">low13week</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">high13week</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">low26week</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">high26week</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">low52week</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">high52week</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">bidYield</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">askYield</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">lastYield</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">markPrice</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">halted</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">rtHistVolatility</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">rtVolume</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">rtTradeVolume</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">rtTime</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">avVolume</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">tradeCount</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">tradeRate</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">volumeRate</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">shortableShares</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">indexFuturePremium</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">futuresOpenInterest</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">putOpenInterest</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">callOpenInterest</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">putVolume</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">callVolume</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">avOptionVolume</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">histVolatility</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">impliedVolatility</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">dividends</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dividends</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">fundamentalRatios</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">FundamentalRatios</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">ticks</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TickData</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">default_factory</span><span class="o">=</span><span class="nb">list</span><span class="p">)</span>
    <span class="n">tickByTicks</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span>
        <span class="n">TickByTickAllLast</span><span class="p">,</span> <span class="n">TickByTickBidAsk</span><span class="p">,</span> <span class="n">TickByTickMidPoint</span><span class="p">]]</span> <span class="o">=</span> \
        <span class="n">field</span><span class="p">(</span><span class="n">default_factory</span><span class="o">=</span><span class="nb">list</span><span class="p">)</span>
    <span class="n">domBids</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">DOMLevel</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">default_factory</span><span class="o">=</span><span class="nb">list</span><span class="p">)</span>
    <span class="n">domAsks</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">DOMLevel</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">default_factory</span><span class="o">=</span><span class="nb">list</span><span class="p">)</span>
    <span class="n">domTicks</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">MktDepthData</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">default_factory</span><span class="o">=</span><span class="nb">list</span><span class="p">)</span>
    <span class="n">bidGreeks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">OptionComputation</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">askGreeks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">OptionComputation</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">lastGreeks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">OptionComputation</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">modelGreeks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">OptionComputation</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">auctionVolume</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">auctionPrice</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">auctionImbalance</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">regulatoryImbalance</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">bboExchange</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">snapshotPermissions</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">__post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updateEvent</span> <span class="o">=</span> <span class="n">TickerUpdateEvent</span><span class="p">(</span><span class="s1">&#39;updateEvent&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">other</span>

    <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="fm">__repr__</span> <span class="o">=</span> <span class="n">dataclassRepr</span>
    <span class="fm">__str__</span> <span class="o">=</span> <span class="n">dataclassRepr</span>

<div class="viewcode-block" id="Ticker.hasBidAsk"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Ticker.hasBidAsk">[docs]</a>    <span class="k">def</span> <span class="nf">hasBidAsk</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;See if this ticker has a valid bid and ask.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bid</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">isNan</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bid</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">bidSize</span> <span class="o">&gt;</span> <span class="mi">0</span>
            <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">ask</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">isNan</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ask</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">askSize</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span></div>

<div class="viewcode-block" id="Ticker.midpoint"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Ticker.midpoint">[docs]</a>    <span class="k">def</span> <span class="nf">midpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return average of bid and ask, or NaN if no valid bid and ask</span>
<span class="sd">        are available.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bid</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">ask</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.5</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hasBidAsk</span><span class="p">()</span> <span class="k">else</span> <span class="n">nan</span></div>

<div class="viewcode-block" id="Ticker.marketPrice"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Ticker.marketPrice">[docs]</a>    <span class="k">def</span> <span class="nf">marketPrice</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the first available one of</span>

<span class="sd">        * last price if within current bid/ask or no bid/ask available;</span>
<span class="sd">        * average of bid and ask (midpoint).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hasBidAsk</span><span class="p">():</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bid</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">last</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ask</span><span class="p">:</span>
                <span class="n">price</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">last</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">price</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">midpoint</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">price</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">last</span>
        <span class="k">return</span> <span class="n">price</span></div></div>


<div class="viewcode-block" id="TickerUpdateEvent"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TickerUpdateEvent">[docs]</a><span class="k">class</span> <span class="nc">TickerUpdateEvent</span><span class="p">(</span><span class="n">Event</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

<div class="viewcode-block" id="TickerUpdateEvent.trades"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TickerUpdateEvent.trades">[docs]</a>    <span class="k">def</span> <span class="nf">trades</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Tickfilter&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Emit trade ticks.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Tickfilter</span><span class="p">((</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">48</span><span class="p">,</span> <span class="mi">68</span><span class="p">,</span> <span class="mi">71</span><span class="p">),</span> <span class="bp">self</span><span class="p">)</span></div>

<div class="viewcode-block" id="TickerUpdateEvent.bids"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TickerUpdateEvent.bids">[docs]</a>    <span class="k">def</span> <span class="nf">bids</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Tickfilter&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Emit bid ticks.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Tickfilter</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">66</span><span class="p">,</span> <span class="mi">69</span><span class="p">),</span> <span class="bp">self</span><span class="p">)</span></div>

<div class="viewcode-block" id="TickerUpdateEvent.asks"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TickerUpdateEvent.asks">[docs]</a>    <span class="k">def</span> <span class="nf">asks</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Tickfilter&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Emit ask ticks.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Tickfilter</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">67</span><span class="p">,</span> <span class="mi">70</span><span class="p">),</span> <span class="bp">self</span><span class="p">)</span></div>

<div class="viewcode-block" id="TickerUpdateEvent.bidasks"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TickerUpdateEvent.bidasks">[docs]</a>    <span class="k">def</span> <span class="nf">bidasks</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Tickfilter&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Emit bid and ask ticks.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Tickfilter</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">66</span><span class="p">,</span> <span class="mi">69</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">67</span><span class="p">,</span> <span class="mi">70</span><span class="p">),</span> <span class="bp">self</span><span class="p">)</span></div>

<div class="viewcode-block" id="TickerUpdateEvent.midpoints"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TickerUpdateEvent.midpoints">[docs]</a>    <span class="k">def</span> <span class="nf">midpoints</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;Tickfilter&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Emit midpoint ticks.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Midpoints</span><span class="p">((),</span> <span class="bp">self</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="Tickfilter"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Tickfilter">[docs]</a><span class="k">class</span> <span class="nc">Tickfilter</span><span class="p">(</span><span class="n">Op</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Tick filtering event operators that ``emit(time, price, size)``.&quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;_tickTypes&#39;</span><span class="p">,)</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tickTypes</span><span class="p">,</span> <span class="n">source</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">Op</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tickTypes</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">tickTypes</span><span class="p">)</span>

<div class="viewcode-block" id="Tickfilter.on_source"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Tickfilter.on_source">[docs]</a>    <span class="k">def</span> <span class="nf">on_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ticker</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">ticker</span><span class="o">.</span><span class="n">ticks</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">tickType</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_tickTypes</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">time</span><span class="p">,</span> <span class="n">t</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="n">t</span><span class="o">.</span><span class="n">size</span><span class="p">)</span></div>

<div class="viewcode-block" id="Tickfilter.timebars"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Tickfilter.timebars">[docs]</a>    <span class="k">def</span> <span class="nf">timebars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timer</span><span class="p">:</span> <span class="n">Event</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;TimeBars&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Aggregate ticks into time bars, where the timing of new bars</span>
<span class="sd">        is derived from a timer event.</span>
<span class="sd">        Emits a completed :class:`Bar`.</span>

<span class="sd">        This event stores a :class:`BarList` of all created bars in the</span>
<span class="sd">        ``bars`` property.</span>

<span class="sd">        Args:</span>
<span class="sd">            timer: Event for timing when a new bar starts.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">TimeBars</span><span class="p">(</span><span class="n">timer</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span></div>

<div class="viewcode-block" id="Tickfilter.tickbars"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Tickfilter.tickbars">[docs]</a>    <span class="k">def</span> <span class="nf">tickbars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;TickBars&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Aggregate ticks into bars that have the same number of ticks.</span>
<span class="sd">        Emits a completed :class:`Bar`.</span>

<span class="sd">        This event stores a :class:`BarList` of all created bars in the</span>
<span class="sd">        ``bars`` property.</span>

<span class="sd">        Args:</span>
<span class="sd">            count: Number of ticks to use to form one bar.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">TickBars</span><span class="p">(</span><span class="n">count</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span></div>

<div class="viewcode-block" id="Tickfilter.volumebars"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Tickfilter.volumebars">[docs]</a>    <span class="k">def</span> <span class="nf">volumebars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;VolumeBars&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Aggregate ticks into bars that have the same volume.</span>
<span class="sd">        Emits a completed :class:`Bar`.</span>

<span class="sd">        This event stores a :class:`BarList` of all created bars in the</span>
<span class="sd">        ``bars`` property.</span>

<span class="sd">        Args:</span>
<span class="sd">            count: Number of ticks to use to form one bar.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">VolumeBars</span><span class="p">(</span><span class="n">volume</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="Midpoints"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Midpoints">[docs]</a><span class="k">class</span> <span class="nc">Midpoints</span><span class="p">(</span><span class="n">Tickfilter</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

<div class="viewcode-block" id="Midpoints.on_source"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Midpoints.on_source">[docs]</a>    <span class="k">def</span> <span class="nf">on_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ticker</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">ticker</span><span class="o">.</span><span class="n">ticks</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">ticker</span><span class="o">.</span><span class="n">time</span><span class="p">,</span> <span class="n">ticker</span><span class="o">.</span><span class="n">midpoint</span><span class="p">(),</span> <span class="mi">0</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="Bar"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.Bar">[docs]</a><span class="nd">@dataclass</span>
<span class="k">class</span> <span class="nc">Bar</span><span class="p">:</span>
    <span class="n">time</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">datetime</span><span class="p">]</span>
    <span class="nb">open</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">high</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">low</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">close</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">nan</span>
    <span class="n">volume</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span></div>


<div class="viewcode-block" id="BarList"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.BarList">[docs]</a><span class="k">class</span> <span class="nc">BarList</span><span class="p">(</span><span class="n">List</span><span class="p">[</span><span class="n">Bar</span><span class="p">]):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updateEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;updateEvent&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span> <span class="ow">is</span> <span class="n">other</span>

    <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span></div>


<div class="viewcode-block" id="TimeBars"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TimeBars">[docs]</a><span class="k">class</span> <span class="nc">TimeBars</span><span class="p">(</span><span class="n">Op</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;_timer&#39;</span><span class="p">,</span> <span class="s1">&#39;bars&#39;</span><span class="p">,)</span>
    <span class="vm">__doc__</span> <span class="o">=</span> <span class="n">Tickfilter</span><span class="o">.</span><span class="n">timebars</span><span class="o">.</span><span class="vm">__doc__</span>

    <span class="n">bars</span><span class="p">:</span> <span class="n">BarList</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timer</span><span class="p">,</span> <span class="n">source</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">Op</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timer</span> <span class="o">=</span> <span class="n">timer</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timer</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_on_timer</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_on_timer_done</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bars</span> <span class="o">=</span> <span class="n">BarList</span><span class="p">()</span>

<div class="viewcode-block" id="TimeBars.on_source"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TimeBars.on_source">[docs]</a>    <span class="k">def</span> <span class="nf">on_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">time</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">bar</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">isNan</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">open</span><span class="p">):</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">open</span> <span class="o">=</span> <span class="n">bar</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">bar</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">price</span>
        <span class="n">bar</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">high</span><span class="p">,</span> <span class="n">price</span><span class="p">)</span>
        <span class="n">bar</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">low</span><span class="p">,</span> <span class="n">price</span><span class="p">)</span>
        <span class="n">bar</span><span class="o">.</span><span class="n">close</span> <span class="o">=</span> <span class="n">price</span>
        <span class="n">bar</span><span class="o">.</span><span class="n">volume</span> <span class="o">+=</span> <span class="n">size</span>
        <span class="n">bar</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_on_timer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">time</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">:</span>
            <span class="n">bar</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">isNan</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">close</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">bar</span><span class="o">.</span><span class="n">open</span> <span class="o">=</span> <span class="n">bar</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">bar</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">bar</span><span class="o">.</span><span class="n">close</span> <span class="o">=</span> \
                    <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">close</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">bar</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Bar</span><span class="p">(</span><span class="n">time</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_on_timer_done</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timer</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timer</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_done</span><span class="p">()</span></div>


<div class="viewcode-block" id="TickBars"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TickBars">[docs]</a><span class="k">class</span> <span class="nc">TickBars</span><span class="p">(</span><span class="n">Op</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;_count&#39;</span><span class="p">,</span> <span class="s1">&#39;bars&#39;</span><span class="p">)</span>
    <span class="vm">__doc__</span> <span class="o">=</span> <span class="n">Tickfilter</span><span class="o">.</span><span class="n">tickbars</span><span class="o">.</span><span class="vm">__doc__</span>

    <span class="n">bars</span><span class="p">:</span> <span class="n">BarList</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">source</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">Op</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o">=</span> <span class="n">count</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bars</span> <span class="o">=</span> <span class="n">BarList</span><span class="p">()</span>

<div class="viewcode-block" id="TickBars.on_source"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.TickBars.on_source">[docs]</a>    <span class="k">def</span> <span class="nf">on_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">time</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">count</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_count</span><span class="p">:</span>
            <span class="n">bar</span> <span class="o">=</span> <span class="n">Bar</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bar</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">bar</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">high</span><span class="p">,</span> <span class="n">price</span><span class="p">)</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">low</span><span class="p">,</span> <span class="n">price</span><span class="p">)</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">close</span> <span class="o">=</span> <span class="n">price</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">volume</span> <span class="o">+=</span> <span class="n">size</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="n">bar</span><span class="o">.</span><span class="n">count</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_count</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="VolumeBars"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.VolumeBars">[docs]</a><span class="k">class</span> <span class="nc">VolumeBars</span><span class="p">(</span><span class="n">Op</span><span class="p">):</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;_volume&#39;</span><span class="p">,</span> <span class="s1">&#39;bars&#39;</span><span class="p">)</span>
    <span class="vm">__doc__</span> <span class="o">=</span> <span class="n">Tickfilter</span><span class="o">.</span><span class="n">volumebars</span><span class="o">.</span><span class="vm">__doc__</span>

    <span class="n">bars</span><span class="p">:</span> <span class="n">BarList</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">source</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">Op</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_volume</span> <span class="o">=</span> <span class="n">volume</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bars</span> <span class="o">=</span> <span class="n">BarList</span><span class="p">()</span>

<div class="viewcode-block" id="VolumeBars.on_source"><a class="viewcode-back" href="../../api.html#ib_insync.ticker.VolumeBars.on_source">[docs]</a>    <span class="k">def</span> <span class="nf">on_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">time</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">volume</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_volume</span><span class="p">:</span>
            <span class="n">bar</span> <span class="o">=</span> <span class="n">Bar</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bar</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">bar</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">high</span><span class="p">,</span> <span class="n">price</span><span class="p">)</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">low</span><span class="p">,</span> <span class="n">price</span><span class="p">)</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">close</span> <span class="o">=</span> <span class="n">price</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">volume</span> <span class="o">+=</span> <span class="n">size</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="n">bar</span><span class="o">.</span><span class="n">volume</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_volume</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bars</span><span class="p">)</span></div></div>
</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2023, Ewald de Wit.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>